// 创建人：刘国宾
// 创建日期：2020-02-26
// 解决问题：vue中导出Excel

import axios from "axios";
import store from '@/store'
import { getToken } from '@/utils/auth'

let download= {
  exportExcel: function (url, data,filename) {
    axios({
      method: "post",
      baseURL: "api",
      withCredentials: true,
      timeout: 5000 ,
      url: url,
      headers: {
        token:store.getters.token?getToken():null
      },
      data: data,
      responseType: "arraybuffer"
    })
      .then(res => {
        let file_Data =  new Blob([res.data], {type: 'application/octet-stream'});
        let file_name = filename||this.getDateTime()+".xls";
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
          window.navigator.msSaveOrOpenBlob(file_Data, file_name);
        }
        else {
          let link = document.createElement('a')
          link.href = window.URL.createObjectURL(file_Data)
          link.download = file_name;
          link.click();
          link.remove();
        }
      });
  }
  ,
  getDateTime:function(){
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return year+''+month+''+day+''+hour+''+minute+''+second
  }
}
export default download;
